package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    UserService userService;

    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    /*新增*/
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
            return new Result(true, "加入用户信息成功");
        } catch (Exception e) {
            return new Result(false,"加入用户信息失败");
        }
    }

    @RequestMapping("/del")
    public Result del(Integer user_id) {

        try {
            userService.del(user_id);
            return new Result(true,"删除角色成功");
        } catch (Exception e) {
            return new Result(false, "删除角色失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer user_id) {
        try {
            User user = userService.findById(user_id);
            return new Result(true, "查询角色成功", user);
        } catch (Exception e) {
            return new Result(false, "查询角色失败");
        }
    }

    //根据检查组合id查询对应的所有角色id
    @RequestMapping("/findRolesIdsByUserId")
    public Result findPermissionIdsByRoleId(Integer user_id) {
        try {
            List<Integer> roleIds = userService.findRolesIdsByUserId(user_id);
            return new Result(true, "查询角色成功", roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }

    /*更新角色信息*/
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
            return new Result(true, "修改用户成功");
        } catch (Exception e) {
            return new Result(false, "修改用户失败");
        }

    }

}
